import { createRequest, RequestConfig } from "@/pkg/requests";

/**
 * Response 查询返回类型约束
 *
 * 通过 axios 的泛型约束限定 ajax 返回的数据格式。
 */
export interface Response<T = any> {
  success: boolean;
  query?: {
    count: number;
    results: Array<T>;
    limit: number;
    offset: number;
    page: number;
  };
  stat?: {
    count: number;
    finished: Array<any>;
    notFinish: Array<any>;
  };
  object?: T;
  message?: string;
  errors?: {
    [propName: string]: Array<string>;
  };
}

const baseURL = import.meta.env.VITE_BASE_URL ?? "/";

const customRequest = createRequest({
  baseURL,
  timeout: 15000,
});
/**
 * request
 *
 * @description 默认的全局 ajax 请求函数，算是一个 Response<T> 的快捷方式
 * @param url {string}
 *
 */
export const request = <T = any>(url: string, options: RequestConfig = {}) =>
  customRequest<Response<T>>(url, options);
